package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.baoyz.widget.PullRefreshLayout;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.adpter.SocailAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.FindTopAll;
import com.lanou.dllo.findfood.entity.FindTopResult;
import com.lanou.dllo.findfood.dialog.WebProgressDialog;
import com.lanou.dllo.findfood.util.NewsClickListener;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/31.
 */
public class FindLifeActivity extends Activity implements NewsClickListener {
private RecyclerView recyclerView;
private SocailAdpter adpter;
private TextView NewsTitle;
    private PullRefreshLayout SocialswipeRefresh;
    private ImageView back_pressed;
private  int id=3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scocal);
        initView();
        initData();
    }
    private  void   initView(){
        recyclerView = (RecyclerView) findViewById(R.id.socialRecyclerView);
        GridLayoutManager manager = new GridLayoutManager(this,1);
        recyclerView.setLayoutManager(manager);
        NewsTitle = (TextView) findViewById(R.id.NewsTitle);
        NewsTitle.setText("生活贴士");
        adpter = new SocailAdpter();
        adpter.setItemLitemListener(this);
        recyclerView.setAdapter(adpter);
        back_pressed= (ImageView) findViewById(R.id.back_pressed);
        back_pressed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        SocialswipeRefresh = (PullRefreshLayout) findViewById(R.id.SocialswipeRefresh);
        SocialswipeRefresh.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                SocialswipeRefresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SocialswipeRefresh.setRefreshing(false);
                    }
                }, 4000);
            }
        });

    }

    private void initData() {
        final WebProgressDialog dialog = new WebProgressDialog(this,R.id.animaImgweb);
        dialog.show();

//"id=6&page=1&rows=15"
        String httpUrl = "http://apis.baidu.com/tngou/info/list";
        String httpArg = "id="+ id+"&page=1&rows=15";
        String path = httpUrl + "?" + httpArg;
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                FindTopAll all = gson.fromJson(response, FindTopAll.class);
                if (all != null) {
                    List<FindTopResult> result = all.getTngou();
                    adpter.addData(result);
                    dialog.dismiss();



                }
                //

            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                error.printStackTrace();
                ;
            }
        }

        ) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };

        VolleySington.getInstance().addRequest(request);


    }



    @Override
    public void itemClickListener(FindTopResult result) {
        Intent intent = new Intent(this,NewsDesiptionActivity.class);
        intent.putExtra("id",result.getId());
        startActivity(intent);
    }
}